package com.example.mobile2121606109ycg0527homework;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class SchoolRViewAdapter extends RecyclerView.Adapter<SchoolRViewAdapter.ViewHolder> {

    private List<School> list;

    public SchoolRViewAdapter(List<School> list) {
        this.list = list;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvSchoolSchoolName, tvSchoolAddress;
        Button btnSchoolUpdate, btnSchoolDelete;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvSchoolSchoolName = itemView.findViewById(R.id.tvSchoolSchoolName);
            tvSchoolAddress = itemView.findViewById(R.id.tvSchoolAddress);
            btnSchoolUpdate = itemView.findViewById(R.id.btnSchoolUpdate);
            btnSchoolDelete = itemView.findViewById(R.id.btnSchoolDelete);

        }
    }
    @NonNull
    @Override
    public SchoolRViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_rv_main_school, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SchoolRViewAdapter.ViewHolder holder, int position) {
        School school = list.get(position);
        holder.tvSchoolSchoolName.setText(school.getSchoolName());
        holder.tvSchoolAddress.setText(school.getAddress());
        holder.btnSchoolDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new AlertDialog.Builder(view.getContext())
                        .setTitle("是否删除数据")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                new MyDataAdapter().delete(view.getContext(), list.get(position).getSchoolName());

                                list.clear();
                                list.addAll(new MyDataAdapter().QueryAll(view.getContext()));
                                notifyDataSetChanged();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        })
                        .create()
                        .show();

            }
        });
        holder.btnSchoolUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final View dialogView = LayoutInflater.from(view.getContext()).inflate(R.layout.dialog_insert, null);

                final EditText edtTxtDialogSchoolName = dialogView.findViewById(R.id.edtTxtDialogSchoolName);
                final EditText edtTxtDialogAddress = dialogView.findViewById(R.id.edtTxtDialogAddress);

                edtTxtDialogSchoolName.setText(list.get(position).getSchoolName());
                edtTxtDialogAddress.setText(list.get(position).getAddress());
                new AlertDialog.Builder(view.getContext())
                        .setTitle("修改数据")
                        .setView(dialogView)
                        .setPositiveButton("修改", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                String schoolName = edtTxtDialogSchoolName.getText().toString().trim();
                                String address = edtTxtDialogAddress.getText().toString().trim();

                                new MyDataAdapter().update(view.getContext(),
                                        list.get(position).getSchoolName(),
                                        new School(schoolName, address));

                                list.clear();
                                list.addAll(new MyDataAdapter().QueryAll(view.getContext()));
                                notifyDataSetChanged();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        })
                        .create()
                        .show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
